package com.wangjw.redis;

import com.wangjw.util.PropertiesUtil;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.util.Hashing;
import redis.clients.util.Sharded;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangjw on 18/5/7.
 */
public class RedisShardedPool {

    //sharded jedis连接池
    private static ShardedJedisPool pool;

    private static Integer redisPort = Integer.parseInt(PropertiesUtil.getProperty("redis.port"));

    //最大连接数
    private static Integer maxTotal = Integer.parseInt(PropertiesUtil.getProperty("redis.max.total", "20"));

    //在jedispool中最大的idle状态(空闲的)jedis实例的个数
    private static Integer maxIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.max.idle", "10"));

    //在jedispool中最小的idle状态(空闲的)jedis实例的个数
    private static Integer minIdle = Integer.parseInt(PropertiesUtil.getProperty("redis.min.idle", "2"));

    //在borrow一个jedis实例的时候，是否进行验证操作，如果赋值为true，则得到的jedis实例肯定是可以用的
    private static Boolean testOnBorrow = Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.borrow", "true"));

    //在return一个jedis实例的时候，是否进行验证操作，如果赋值为true，则放回jedispool的jedis实例肯定是可以用的
    private static Boolean testOnReturn = Boolean.parseBoolean(PropertiesUtil.getProperty("redis.test.return", "false"));

    private static String redis1Ip = PropertiesUtil.getProperty("redis1.ip");
    private static String redis1Password = PropertiesUtil.getProperty("redis1.password");

    private static String redis2Ip = PropertiesUtil.getProperty("redis2.ip");
    private static String redis2Password = PropertiesUtil.getProperty("redis2.password");

    private static String redis3Ip = PropertiesUtil.getProperty("redis3.ip");
    private static String redis3Password = PropertiesUtil.getProperty("redis3.password");

    private static void initPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setTestOnBorrow(testOnBorrow);
        config.setTestOnReturn(testOnReturn);
        config.setBlockWhenExhausted(true); //连接耗尽时是否阻塞，false会抛出异常，true阻塞直到超时，默认为true

        JedisShardInfo info1 = new JedisShardInfo(redis1Ip, redisPort, 1000 * 2);
        info1.setPassword(redis1Password);

        JedisShardInfo info2 = new JedisShardInfo(redis2Ip, redisPort, 1000 * 2);
        info2.setPassword(redis2Password);

        JedisShardInfo info3 = new JedisShardInfo(redis3Ip, redisPort, 1000 * 2);
        info3.setPassword(redis3Password);

        List<JedisShardInfo> jedisShardInfoList = new ArrayList<>(2);
        jedisShardInfoList.add(info1);
        jedisShardInfoList.add(info2);
        jedisShardInfoList.add(info3);
        pool = new ShardedJedisPool(config, jedisShardInfoList, Hashing.MURMUR_HASH, Sharded.DEFAULT_KEY_TAG_PATTERN);
    }

    static {
        initPool();
    }

    public static ShardedJedis getJedis() {
        return pool.getResource();
    }

    public static void returnResource(ShardedJedis jedis) {
        pool.returnResource(jedis);
    }

    public static void returnBrokenResource(ShardedJedis jedis) {
        pool.returnBrokenResource(jedis);
    }

    public static void main(String[] args) {
        ShardedJedis jedis = pool.getResource();
        for (int i = 0; i < 50; i++) {
            jedis.set("key-" + i, "value-" + i);
        }
        returnResource(jedis);

        System.out.println("program is end");
    }
}
